<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UnitTest
 *
 * @author pleblanc
 */
abstract class UnitTestCase {
    private static $_registry;
    private static $_benchmark;


    private static function _setReferencers($result, $comment)
    {

        $temp = debug_backtrace();

        self::$_registry[$temp[2]['class']][$temp[2]['function']][] = array(
            'line' => $temp[1]['line'],
            'test' => $temp[1]['function'],
            'result' => $result, 'comment' => $comment,
            'reference' =>$temp[5]['function']
            );
    }
    public static function assertTrue($val, $comment = '')
    {
        self::_setReferencers($val, $comment);
    }
    public static function report($flush = false)
    {
        ?>
            <style type="text/css">
                .testTable{
                    background-color: #DDD;
                    width: 30%;
                }
                .testTable td{
                    border:1px solid #CCC;
                }
                .testTable tr.failed td{
                    border:1px solid #FBB;
                    background-color:#FDD;
                }
                .testTable tr.passed td{
                    border:1px solid #BFB;
                    background-color:#DFD;
                }
                .subComponent{
                    margin-left: 25px;
                }
                
            </style>
            <?php foreach (self::$_registry as $class => $classContent) : ?>
                <h2><?php echo $class ?></h2>
                <?php foreach ($classContent as $function => $functionTests) :?>
                    <?php $subcomp = strpos($function, 'test') === false ? 'subComponent' : false ; ?>
                    <h3 class ="<?php echo $subcomp ?>"><?php echo $function ?></h3>
                    <table class="testTable <?php echo $subcomp?>">
                        <thead>
                            <tr>
                                <th>Line #</th>
                                <?php if($subcomp):?>
                                    <th>Referencer</th>
                                <?php endif; ?>
                                <th>Test</th>
                                <th>Result</th>
                            </tr>
                        </thead>
                        <?php foreach ($functionTests as $key => $val): ?>
                        <tr class="<?php echo $val['result'] ? 'passed' : 'failed' ?>">
                            <td><?php echo $val['line'] ?></td>
                            <?php if($subcomp):?>
                                <td><?php print_r($val['reference']) ?></td>
                            <?php endif; ?>
                            <td><?php echo $val['test'] . ($val['comment'] == '' ? '' : ' - ' . $val['comment']); ?></td>
                            <td><?php echo $val['result'] ? 'Passed' : 'Failed'; ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </table>
                <?php endforeach; ?>
            <?php endforeach; ?>
    
                    <hr/>
                    <?php echo self::$_benchmark['timeTaken']?>
        <?php

    }

    public static function run()
    {
        self::$_benchmark['startTime'] = microtime();

        foreach(get_class_methods(get_called_class()) as $function) {
            if(strpos($function, 'test') !== false)
                call_user_func(array(get_called_class(),$function));
        }

        self::$_benchmark['endTime'] = microtime();
        self::$_benchmark['timeTaken'] = self::$_benchmark['endTime'] - self::$_benchmark['startTime'];
    }

}


?>
